package com.imay.live.view.custom;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.imay.live.R;
import com.imay.live.utils.AnimationUtil;
import com.imay.live.utils.ViewUtil;

/**
 * Created by jeaman on 2017/3/24.
 * 直播UI
 */

public class RoomPushShortView extends RoomBaseView implements CountdownView.CountDownTimerListener, RoomChallengeView.OnChallengeShowListener {
    private View mMoreActionRootView;   //更多按钮容器
    private CountdownView mPushCountDown;
    private RoomChallengeView mRoomChallengeView;
    private MusicPlayView musicPlayView;

    private OnPushActionListener mListener;
    private CheckBox live_room_iv_more;

    public RoomPushShortView(Context context) {
        super(context);
    }

    public RoomPushShortView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoomPushShortView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RoomPushShortView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    protected int getLayoutId() {
        return R.layout.view_room_push_short;
    }

    protected void init(Context context) {
        super.init(context);
        mTopRootView = findViewById(R.id.live_room_rl_top);
        mActionRootView = findViewById(R.id.live_room_rl_bottom);
        mMoreActionRootView = findViewById(R.id.live_room_ll_action_more);
        mPushCountDown = (CountdownView) findViewById(R.id.live_room_cdv_push_countdown);
        mRoomChallengeView = (RoomChallengeView) findViewById(R.id.live_room_rsv_chat);
        musicPlayView = (MusicPlayView) findViewById(R.id.live_room_iv_music);
        live_room_iv_more= (CheckBox) findViewById(R.id.live_room_iv_more);

        mRoomChallengeView.setOnChallengeShowListener(this);
        mRoomChallengeView.setChallengeContentVisibility(true);    //默认显示挑战内容


        //超短播替换水印
//        ImageView waterMarkIv = (ImageView) findViewById(R.id.live_room_iv_watermark);
//        waterMarkIv.setImageResource(R.mipmap.ico_03_chaodanbo);

        initListener();
    }

    protected void initListener() {
//        super.initListener();
//        findViewById(R.id.live_room_iv_game).setOnClickListener(this);
        findViewById(R.id.live_room_iv_gift).setOnClickListener(this);
//        findViewById(R.id.live_room_iv_screenshot).setOnClickListener(this);
//        findViewById(R.id.live_room_iv_share).setOnClickListener(this);
        findViewById(R.id.live_room_iv_camera).setOnClickListener(this);
        findViewById(R.id.live_room_iv_beauty).setOnClickListener(this);
        musicPlayView.setOnClickListener(this);
        live_room_iv_more.setOnClickListener(this);



    }

    //开始倒计时
    public void startCountDown(long millisInFuture) {
        mPushCountDown.setVisibility(VISIBLE);
        mPushCountDown.startCountDown(millisInFuture, this);
    }

    //取消倒计时
    public void cancelCountDown() {
        mPushCountDown.cancelCountDown();
    }

    //隐藏除cocos所有view
    public void hideAllViewWithoutCocos() {
        mTopRootView.setVisibility(GONE);
        mChatRootView.setVisibility(GONE);
        mActionRootView.setVisibility(GONE);
        mCloseView.setVisibility(GONE);
    }

    public void showAllView() {
        mTopRootView.setVisibility(VISIBLE);
        mChatRootView.setVisibility(VISIBLE);
        mActionRootView.setVisibility(VISIBLE);
        mCloseView.setVisibility(VISIBLE);
    }

    @Override
    public void reset() {
        super.reset();
        setChallengeContentVisibility(true);
        setMessageViewVisibility(false);
    }

    @Override
    protected void showInputView() {
        super.showInputView();
        setChallengeContentVisibility(false);//隐藏挑战题目
        setMessageViewVisibility(true); //显示公屏区
    }

    @Override
    public void setBottomViewVisibility(boolean visible) {
        setActionViewVisibility(visible);
        if (!mRoomChallengeView.isShowChallengeContent()) {
            setMessageViewVisibility(visible);
        }
        int visibility = visible ? VISIBLE : INVISIBLE;
        ViewUtil.startVisibilityAnimation(mRoomChallengeView, visibility);
    }

    // 设置挑战标题
    public void setChallengeTitle(String challengeTitle) {
        mRoomChallengeView.setChallengeTitle(challengeTitle);
    }

    // 设置挑战内容
    public void setChallengeContent(String challengeContent) {
        mRoomChallengeView.setChallengeContent(challengeContent);
    }

    // 隐藏/显示挑战内容
    public void setChallengeContentVisibility(boolean visible) {
        mRoomChallengeView.setChallengeContentVisibility(visible);
    }

    public void setOnPushActionListener(OnPushActionListener pushActionListener) {
        super.setOnActionListener(pushActionListener);
        this.mListener = pushActionListener;
    }

    //切换更多按钮
    private void toggleMoreAction() {

        if (mMoreActionRootView.getVisibility() == VISIBLE) {
            mMoreActionRootView.setVisibility(GONE);
            AnimationUtil.startToBottomAnimation(mMoreActionRootView, 250, null);
            live_room_iv_more.setChecked(false);
        } else {
            mMoreActionRootView.setVisibility(VISIBLE);
            AnimationUtil.startFromBottomAnimation(mMoreActionRootView, 250, null);
            live_room_iv_more.setChecked(true);

        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
//            case R.id.live_room_iv_game:
//                mListener.onGameClick();
//                break;
            case R.id.live_room_iv_gift:
                mListener.onGiftClick();
                break;
            case R.id.live_room_iv_more:
                toggleMoreAction();
                break;
//            case R.id.live_room_iv_screenshot:
//                mListener.onScreenShotClick();
//                break;
            case R.id.live_room_iv_music:
                mListener.onMusicClick();
                break;
            case R.id.live_room_iv_camera:
                mListener.onCameraClick();
                break;
            case R.id.live_room_iv_beauty:
                mListener.onBeautyClick();
                break;
        }
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mListener.onCountdownTick(millisUntilFinished);
    }

    @Override
    public void onFinish() {
        mListener.onCountdownFinish();
    }

    @Override
    public void onChallengeShow(boolean show) {
        //挑战题目隐藏显示时显示公屏区
        setMessageViewVisibility(!show);
    }

    public interface OnPushActionListener extends OnActionListener {
//        void onGameClick();

        void onGiftClick();

//        void onScreenShotClick();

        void onMusicClick();

        void onCameraClick();

        void onBeautyClick();

        void onCountdownFinish();
        void onCountdownTick(long millisUntilFinished);
    }

    public void setPlayViewVisible(boolean visible){
        musicPlayView.setVisibility(visible?VISIBLE:GONE);

    }
    public void setMusicViewAnim(boolean isPlaying){
        if (isPlaying){
            musicPlayView.startRotationAnim();
        }else{
            musicPlayView.pauseRotationAnim();
        }

    }
}
